﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WxSDK.Model;

namespace WxSDK
{
   public  class WeChatMenu
    {
        #region 用户微信菜单
        /// <summary>
        /// 根据Menu列表生成符合微信规范的创建菜单JSon
        /// <para>一级菜单不超过3个，二级菜单不超过5个</para>
        /// </summary>
        /// <param name="access_token">微信唯一标示</param>
        /// <param name="munuList">菜单列表，type为click时url留空，view时key留空</param>
        /// <returns></returns>
        public string CreateMenuJson(IList<wx_menu> munuList)
        {
            string menuJson = "";
            menuJson += "{\"button\":[";
            foreach (wx_menu menu in munuList)
            {
                if (menu.IsSub) //是子菜单
                {
                    menuJson += "{";
                    menuJson += "\"name\":\"" + menu.MenuTitle + "\",";
                    menuJson += "\"sub_button\":[";
                    foreach (var item in menu.Sub_button) //子菜单
                    {
                        menuJson += "{";
                        menuJson += "\"type\":\"" + item.MenuType + "\",";
                        menuJson += "\"name\":\"" + item.MenuTitle + "\",";
                        if (item.MenuKey != null)
                        {
                            menuJson += "\"key\":\"" + item.MenuKey + "\"";
                        }
                        else
                        {
                            menuJson += "\"url\":\"" + item.MenuUrl + "\"";
                        }
                        menuJson += "},";
                    }
                    menuJson = menuJson.TrimEnd(',');
                    menuJson += "]},";
                }
                else
                {
                    menuJson += "{";
                    menuJson += "\"type\":\"" + menu.MenuType + "\",";
                    menuJson += "\"name\":\"" + menu.MenuTitle + "\",";
                    if (menu.MenuKey != null)
                    {
                        menuJson += "\"key\":\"" + menu.MenuKey + "\"";
                    }
                    else
                    {
                        menuJson += "\"url\":\"" + menu.MenuUrl + "\"";
                    }
                    menuJson += "},";
                }
            }
            menuJson = menuJson.TrimEnd(',');
            menuJson += "]}";

            return menuJson;
        }

        /// <summary>
        /// 获取该用户在本系统中的微信菜单Json 用于设置用户菜单
        /// </summary>
        /// <param name="memberid"></param>
        /// <returns></returns>
        public string GetWxMenu(int memberid)
        {
            IList<wx_menu> list = new List<wx_menu>();

            wx_menu m1 = new wx_menu();
            m1.MenuType = "click";
            m1.MenuTitle = "今日歌曲";
            m1.MenuKey = "V1001_TODAY_MUSIC";

            wx_menu m2 = new wx_menu();
            m2.MenuType = "click";
            m2.MenuTitle = "歌手简介";
            m2.MenuKey = "V1001_TODAY_SINGER";



            wx_menu m3 = new wx_menu();
            m3.MenuTitle = "菜单";
            m3.IsSub = true;

            wx_menu m4 = new wx_menu();
            m4.MenuType = "view";
            m4.MenuTitle = "搜索";
            m4.MenuUrl = "http://www.soso.com/";

            wx_menu m5 = new wx_menu();
            m5.MenuType = "view";
            m5.MenuTitle = "视频";
            m5.MenuUrl = "http://v.qq.com";

            wx_menu m6 = new wx_menu();
            m6.MenuType = "click";
            m6.MenuTitle = "赞我们一下";
            m6.MenuKey = "V1001_GOOD";

            m3.Sub_button.Add(m4);
            m3.Sub_button.Add(m5);
            m3.Sub_button.Add(m6);

            list.Add(m3);
            list.Add(m3);
            list.Add(m3);

            string json = CreateMenuJson(list);
            return json;
        }

        
       

        #endregion

    }
}
